package com.xiaoyu.aop.framework.adapter;

import com.xiaoyu.aop.MethodAfterAdvice;
import com.xiaoyu.aop.MethodBeforeAdvice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class MethodAroundAdviceInterceptor implements MethodInterceptor {
    private MethodBeforeAdvice beforeAdvice;
    private MethodAfterAdvice afterAdvice;

    public MethodAroundAdviceInterceptor(MethodBeforeAdvice methodBeforeAdvice, MethodAfterAdvice methodAfterAdvice) {
        this.beforeAdvice = methodBeforeAdvice;
        this.afterAdvice = methodAfterAdvice;
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        beforeAdvice.before(methodInvocation.getMethod(), methodInvocation.getArguments(), methodInvocation.getThis());
        methodInvocation.proceed();
        return afterAdvice.after(methodInvocation.getMethod(), methodInvocation.getArguments(), methodInvocation.getThis());
    }
}
